---
title: "Reading a local file with the File API"
layout: default
section: example
---

<h3>Choose the local(s) zip file(s)</h3>
<p class="note">Note : your browser will process the zip file, don't choose a file too big !</p>
<input type="file" id="file" name="file" multiple /><br />

<div id="error_block" class="alert alert-danger hidden">
  You will need a recent browser to use this demo :(
</div>

<div id="result_block" class="hidden">
  <h3>Content :</h3>
  <div id="result"></div>
</div>
<script type="text/javascript" src="{{site.baseurl}}/test/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
(function () {
  if (!window.FileReader || !window.ArrayBuffer) {
    $("#error_block").removeClass("hidden").addClass("show");
    return;
  }


  var $result = $("#result");
  $("#file").on("change", function(evt) {
    // remove content
    $result.html("");
    // be sure to show the results
    $("#result_block").removeClass("hidden").addClass("show");

    // Closure to capture the file information.
    function handleFile(f) {
      var $title = $("<h4>", {
        text : f.name
      });
      var $fileContent = $("<ul>");
      $result.append($title);
      $result.append($fileContent);
    
      var dateBefore = new Date();
      JSZip.loadAsync(f)
      .then(function(zip) {
        var dateAfter = new Date();
        $title.append($("<span>", {
          text:" (loaded in " + (dateAfter - dateBefore) + "ms)"
        }));


        zip.forEach(function (relativePath, zipEntry) {
          $fileContent.append($("<li>", {
            text : zipEntry.name
          }));
        });
      }, function (e) {
        $fileContent = $("<div>", {
          "class" : "alert alert-danger",
          text : "Error reading " + f.name + " : " + e.message
        });
      });
    }

    var files = evt.target.files;
    for (var i = 0, f; f = files[i]; i++) {
        handleFile(f);
    }
  });
})();
</script>
